<template>
	<view class="cart">
		<view class="header">
			<image src="../../../static/icons/arrl.png" mode=""@tap="back"></image>
			<text>乐居</text>
		</view>
		
		<view class="cart-list">
			<view class="cart-item" v-for="item in cartList" :key="item.id">
				<view class="title">
					<view>
						<evan-checkbox class="check" v-model="item.panChecked" shape="square" @change="jisuan"></evan-checkbox>
					</view>
					<view>
						<image :src="item.productPic" mode=""></image>
					</view>
					<view>
						<view class="cname">{{item.productName}}</view>
						<view class="con">编号:{{item.productSkuId}}价格: ¥{{item.price}}</view>
						<view>数量:{{item.quantity}}</view>
					</view>
				</view>
				<view class="bottom">
					<view @tap="remove(item.id)">-</view>
					<view>{{item.quantity}}</view>
					<view @tap="add(item)">+</view>
				</view>
			</view>
		</view>
		
		<view class="footer">
			<view class="all">
				<view>全选</view>
				<view>
					<evan-checkbox class="check" v-model="allChecked" shape="square" @change="jisuan"></evan-checkbox>
				</view>
			</view>
			<view class="result">
				<view>合计: ¥</view>
				<view>{{total}}</view>
				<view @tap="jumpPay">结算</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mix from "../../../mixins/index.js"
	import { findAllCartItem,delCartItems } from "../../../api/home/cate.js"
	import { addPreOrder } from "../../../api/home/order.js"
	export default {
		mixins:[mix],
		data() {
			return {
				cartList:[],
				total:0,
				orderId:""
			};
		},
		computed:{
			allChecked:{
				get() {
					return this.cartList.every(el => el.panChecked)
				},
				set(val){
					console.log(val)
					this.cartList.forEach(el => el.panChecked = val)
				}
			},
			paramsList() {
				var list = []
				this.cartList.filter(el=>{
					if(el.panChecked) {
						var obj = {}
						obj.cartId = el.id;
						obj.productId = el.memberId;
						obj.productQuantity = el.quantity;
						obj.productSkuId = el.productSkuId
						list.push(obj)
					}
				})
				return list
			}
		},
		methods:{
			jumpPay() {
				addPreOrder({
					orderItemList:this.paramsList
				}).then(res=>{
					console.log(res)
					if (res.success) {
						this.orderId = res.data.orderId
						uni.navigateTo({
							url:`/pages/home/buy/buy?id=${this.orderId}`
						})
					}
				})
			},
			back() {
				uni.navigateBack({
					delta:1
				})
			},
			prices() {
				var total = this.cartList.reduce((total,current)=>{
					if (current.panChecked) {
						return total + current.price * current.quantity
					} else {
						return total
					}
				},0)
				this.total = total
			},
			jisuan() {
				this.prices()
			},
			remove(id) {
				var index = this.cartList.findIndex(el =>el.id == id)
				console.log(index)
				if (this.cartList[index].quantity > 1) {
					this.cartList[index].quantity--
				} else {
					delCartItems([id]).then(res=>{
						console.log("我是删除",res,id)
						if (res.success == true) {
							findAllCartItem().then(res=>{
								this.cartList = res.data.items
								console.log(res)
							})
						}
						
					})
				}
				this.prices()
			},
			add(item) {
				item.quantity++
				this.prices()
			}
		},
		onLoad() {
			this.checkLogins()
		},
		onShow() {
			findAllCartItem().then(res=>{
				res.data.items.forEach(el=>{
					el.panChecked = false
				})
				this.cartList = res.data.items
				console.log(res)
			})
		}
	}
</script>

<style lang="scss" scoped>
	.cart {
		padding-bottom: 160rpx;
	}
.header {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #354e44;
	height: 60rpx;
	padding: 14rpx 6rpx;
	image {
		position: absolute;
		left: 4rpx;
		top: 50%;
		transform: translateY(-50%);
		width: 40rpx;
		height: 40rpx;
	}
	text {
		font-size: 32rpx;
		color: #FFFFFF;
		font-weight: bolder;
	}
}
.footer {
	display: flex;
	justify-content: space-around;
	align-items: center;
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	height: 150rpx;
	background-color: #FFFFFF;
	.all {
		display: flex;
		view {
			&:nth-child(1) {
				font-size: 32rpx;
				color: #000;
				margin-right: 20rpx;
			}
		}
	}
	.result {
		display: flex;
		view {
			line-height: 80rpx;
			&:nth-child(1) {
				font-size: 32rpx;
				color: #000;
			}
			&:nth-child(2) {
				font-size: 32rpx;
				color: #f00;
				margin-right: 10rpx;
			}
			&:nth-child(3) {
				font-size: 36rpx;
				color: #fff;
				width: 208rpx;
				height: 80rpx;
				background-color: #354e44;
				border-radius: 28rpx;
				text-align: center;
			}
		}
	}
}
.cart-list {
		
	.cart-item{
		width: 632rpx;
		padding: 20rpx;
		margin: 50rpx auto;
		background-color: #FFFFFF;
		border-radius: 24rpx;
		.title {
			display: flex;
			align-items: center;
			>view {
				&:nth-child(2) {
					width: 124rpx;
					height: 136rpx;
					margin: 0 20rpx;
					image {
						width: 100%;
						height: 100%;
					}
				}
				&:nth-child(3) {
					font-size: 24rpx;
					color: #8d8d8d;
					.cname {
						font-size: 26rpx;
						color: #3e3e3e;
					}
					.con {
						width: 430rpx;
					}
				}
			}
		}
		.bottom {
			display: flex;
			justify-content: flex-end;
			>view {
				width: 60rpx;
				height: 60rpx;
				background-color: #f1ece7;
				border-radius: 50%;
				margin: 0 6rpx;
				font-size: 30rpx;
				color: #000;
				line-height: 60rpx;
				text-align: center;
				&:nth-child(2) {
					width: 90rpx;
					border-radius: 30rpx;
				}
			}
		}
	}
}
</style>
